package com.car.alliance.ui.mine

import android.annotation.SuppressLint
import android.widget.Toast
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.framework.ShareSDK
import cn.sharesdk.wechat.friends.Wechat
import com.car.alliance.R
import com.car.alliance.base.BaseActivity
import com.car.alliance.mvp.presenter.CategoryDetailPresenter
import com.car.alliance.utils.StatusBarUtil
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.activity_login.*
import java.util.*

class LoginActivity : BaseActivity(){
    private val mPresenter by lazy { CategoryDetailPresenter() }
    override fun layoutId(): Int = R.layout.activity_login

    override fun initData() {


    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initView() {
        //状态栏透明和间距处理
        StatusBarUtil.darkMode(this)
//        StatusBarUtil.setPaddingSmart(this, toolbar)

        vx_login.setOnClickListener {
            loginByWeixin()
        }
    }

    override fun start() {

    }


    /**
     * 微信登陆
     */
    private fun loginByWeixin() {
        val wechat = ShareSDK.getPlatform(Wechat.NAME)
        if (wechat.isAuthValid) {
            //执行此操作就可以移除掉本地授权状态和授权信息
            wechat.removeAccount(true)
        }
        wechat.platformActionListener = object : PlatformActionListener {
            override fun onComplete(platform: Platform, i: Int, hashMap: HashMap<String, Any>) {
                Logger.i("onComplete ---->  WX登录成功$hashMap")
                val platDB = platform.db //获取数平台数据DB
//                Thidelogin("wx", platDB.userId, platDB.userName, platDB.userIcon)
            }

            override fun onError(platform: Platform, i: Int, throwable: Throwable) {
                Logger.i("onError ---->  WX登录失败$throwable")
            }

            override fun onCancel(platform: Platform, i: Int) {
                Logger.i("onCancel ---->  WX登录取消")
            }
        }
        wechat.SSOSetting(false)
        if (!wechat.isClientValid) {
//            ToastUtil.show("微信未安装,请先安装微信", Toast.LENGTH_SHORT)
            return
        }
        authorize(wechat)
    }

    /**
     * 授权
     *
     * @param platform
     */
    private fun authorize(platform: Platform?) {
        if (platform == null) {
            return
        }
        if (platform.isAuthValid) {  //如果授权就删除授权资料
            platform.removeAccount(true)
        }
        platform.showUser(null) //授权并获取用户信息
    }

}